var count = 0;
var markers = new Array();
var idPlant = new Array();
var plantName = new Array();
var scientific = new Array();
var alias = new Array();
var longitude = new Array();
var latitude = new Array();
var images = new Array();
var map;
var xhr;
var isChoosed = false;
function initMap()
{
	var latlng = new google.maps.LatLng(24.437736364171066,118.09754759073257);
	map = new google.maps.Map(document.getElementById('map'), {  
	    zoom: 14,  
	    center: latlng,  
	    mapTypeId: google.maps.MapTypeId.ROADMAP  
	});
//	google.maps.event.addListener(map, 'click', function(event) {
//	    placeMarker(event.latLng);
//	});
	var sw =  new google.maps.LatLng(24.429128461693962,118.07772142648697);
	var ne =  new google.maps.LatLng(24.45523099834168,118.1123082947731);
	//自定义地图的显示范围:厦大周边  
	var strictBounds = new google.maps.LatLngBounds(  
	    sw, ne);  
	google.maps.event.addListener(map, 'dragend',  
	function() {  
	    if (strictBounds.contains(map.getCenter())) return;  
	    var c = map.getCenter(),  
	    x = c.lng(),  
	    y = c.lat(),  
	    maxX = strictBounds.getNorthEast().lng(),  
	    maxY = strictBounds.getNorthEast().lat(),  
	    minX = strictBounds.getSouthWest().lng(),  
	    minY = strictBounds.getSouthWest().lat();  
	    if (x < minX) x = minX;  
	    if (x > maxX) x = maxX;  
	    if (y < minY) y = minY;  
	    if (y > maxY) y = maxY;  
	    map.setCenter(new google.maps.LatLng(y, x));  
	});
	//标志植物地理位置
	for(var i = 0; i < count; i++){
		placeMarker(i);
	}
}
/**
 * 放置植物地理位置图标
 * @param index
 * @return
 */
function placeMarker(index){
	var lng = Number(longitude[index]);
	var lat = Number(latitude[index]);
	var location = new google.maps.LatLng(lat,lng);

	var marker = new google.maps.Marker( {
		position : location,
		map : map
	});
	google.maps.event.addListener(marker, 'click', function() {
		showPlantInfo(index);
		isChoosed = true;
	});
	
	markers[index] = marker;
}
/**
 * 存储地理位置
 */
function storeLocation(lng, lat, idplant, name, scient, ali, img){
	idPlant[count] = idplant;
	plantName[count]= name;
	scientific[count] = scient;
	alias[count] = ali;
	images[count] = img;
	longitude[count] = lng;
	latitude[count] = lat;
	count++;
}
/**
 * 得到植物信息
 */
function showPlantInfo(index){
	var pname = document.getElementById("pname");
	var science = document.getElementById("scientific");
	var a = document.getElementById("alias");
	var more = document.getElementById("more");
	while(pname.hasChildNodes()){
		pname.removeChild(pname.childNodes[0]);
	}
	while(science.hasChildNodes()){
		science.removeChild(science.childNodes[0]);
	}
	while(a.hasChildNodes()){
		a.removeChild(a.childNodes[0]);
	}

	var textNode = document.createTextNode(plantName[index]);
	pname.appendChild(textNode);
	textNode = document.createTextNode(scientific[index]);
	science.appendChild(textNode);
	textNode = document.createTextNode(alias[index]);
	a.appendChild(textNode);
	
	var checkmore = document.getElementById("checkmore");
	checkmore.removeChild(more);
	var html = document.createElement("div");
	html.setAttribute("id","more");
	html.setAttribute("class","name");
	html.innerHTML = "<a href='plantManager-plantInfo.html?plantName=" + plantName[index] +"'>查看更多信息...</a>";
	checkmore.appendChild(html);
	
	var pimage =  document.getElementById("pimage");
//	pimage.style.background = "url(" + images[index]+ ") center no-repeat";
	
	var addimage = document.createElement("div");
	addimage.setAttribute("id","newimage");
	addimage.setAttribute("class","centerimg");
	addimage.innerHTML='<img src="'+ images[index] +'"/>';
	
	var layer = document.getElementById("layer");
	var newimage = document.getElementById("newimage");
	if(layer != null)
		pimage.removeChild(layer);
	if(newimage != null)
		pimage.removeChild(newimage);
	pimage.appendChild(addimage);
	
	var newlayer = document.createElement("div");
	newlayer.setAttribute("class", "cover boxcaption");
	newlayer.setAttribute("id", "layer");
	newlayer.innerHTML = "<h3>" + plantName[index] +"</h3><p><a href='plantManager-plantInfo.html?plantName="
				+ plantName[index] +"'>更多植物信息...</a></p>";
	pimage.appendChild(newlayer);
	document.getElementById('result').scrollIntoView();
}

function showMore(name){
	if(isChoosed){
		location.href="plantManager-plantInfo.html?plantName=" + name;
	}
}